package com.xb.cloud.usercenter.client;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.usercenter.service.PassportService;

/**
 * 
 * @ClassName: EndUserLoginAdapter
 * @Description: 终端用户登录注册
 * @author: 鲜备科技
 * @date: 2017年12月21日 下午6:32:20
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class PassportAdapter extends Adapter {

	@Resource
	PassportService passportServiceApi;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:终端用户登录注册
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param contextt
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	@Override
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;
		long time = System.currentTimeMillis();
		long companyId = context.getCompanyId();

		if ("/h5/passport/sendSms".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			return passportServiceApi.sendSms(companyId, phoneNum);
		}

		if ("/h5/passport/password/login".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String password = bizInfo.get("password");
			return passportServiceApi.loginWithPassword(companyId, phoneNum, password);
		}

		if ("/h5/passport/verifyCode/login".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			return passportServiceApi.loginWithVerifyCode(companyId, phoneNum, verifyCode);
		}

		if ("/h5/passport/register".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			String password = bizInfo.get("password");
			return passportServiceApi.register(companyId, phoneNum, verifyCode, password);
		}

		if ("/h5/passport/resetPassword".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			String password = bizInfo.get("password");
			return passportServiceApi.resetPassword(companyId, phoneNum, verifyCode, password);
		}

		if ("/h5/passport/initUser".equalsIgnoreCase(urlPath)) {
			// TODO:用户初始化，一期只简单初始化密码
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			String password = bizInfo.get("password");
			return passportServiceApi.resetPassword(companyId, phoneNum, verifyCode, password);
		}

		if ("/h5/passport/autoLogin".equalsIgnoreCase(urlPath)) {
			String autoUserToken = bizInfo.get("autoUserToken");
			return passportServiceApi.autoLogin(autoUserToken);
		}

		if ("/h5/passport/logout".equalsIgnoreCase(urlPath)) {
			String userToken = context.getRequestInfo().getUserToken();
			String autoUserToken = bizInfo.get("autoUserToken");
			return passportServiceApi.logout(userToken, autoUserToken);
		}

		System.out.println(System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/passport/**";
	}

}
